package com.arcaneshift.manager.action.game;

import java.util.ArrayList;
import java.util.List;

import com.arcaneshift.dao.action.game.SpellCardDao;
import com.arcaneshift.dao.interfaces.ISpellCardDao;
import com.arcaneshift.manager.interfaces.ISpellCardManager;
import com.arcaneshift.model.action.game.SpellCard;
import com.arcaneshift.security.exceptions.CardException;
import com.common.dao.interfaces.IDao;
import com.common.dao.util.Criteria;
import com.common.dao.util.QueryOperation;
import com.common.manager.abstracts.AbstractManager;
import com.common.security.exceptions.DatabaseException;
import com.common.security.exceptions.ManagerException;

public class SpellCardManager extends AbstractManager<SpellCard> implements ISpellCardManager {

	private ISpellCardDao spellDao;
	
	public SpellCardManager() {
		spellDao = new SpellCardDao();
	}
	
	@Override
	protected IDao<SpellCard> getDao() {
		return spellDao;
	}

	/**
	 * Check if a Spell Card with the provided name exists in the database
	 * 
	 * @param name
	 * @return true/false
	 * @throws ManagerException.DATABASE_EXCEPTION
	 */
	@Override
	public boolean exist(String name) throws ManagerException {
		try {
			List<Criteria> criterias = new ArrayList<Criteria>();
			criterias.add(new Criteria(SpellCard.NAME, name, QueryOperation.EQUALS, true));
			return spellDao.getByCriteria(criterias).size() > 0;
		} catch (DatabaseException databaseException) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
	
	/**
	 * 
	 * @param name
	 * @return
	 * @throws ManagerException.DATABASE_EXCEPTION
	 * @throws CardException.CARD_NOT_FOUND
	 * @throws CardException.MULTIPLE_CARDS_FOUND
	 */
	public SpellCard getSpellCard(String name) throws ManagerException {
		try {
			List<Criteria> criterias = new ArrayList<Criteria>();
			criterias.add(new Criteria(SpellCard.NAME, name, QueryOperation.EQUALS, true));
			List<SpellCard> spells = spellDao.getByCriteria(criterias);
			
			if (spells.size() == 0) {
				throw new CardException(CardException.CARD_NOT_FOUND);
			} else if (spells.size() == 1) {
				return spells.get(0);
			} else {
				throw new CardException(CardException.MULTIPLE_CARDS_FOUND);
			}
		} catch (DatabaseException databaseException) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
}
